﻿namespace YdmCodeArtCoreApplication.Tests
{
    using System.IO;
    using System.Reflection;
    using Abp.EntityFrameworkCore.Configuration;
    using Abp.Modules;
    using Abp.Reflection.Extensions;
    using Abp.TestBase;
    using Castle.MicroKernel.Registration;
    using Castle.Windsor.MsDependencyInjection;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Http;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Moq;
    using Senparc.CO2NET;
    using Senparc.CO2NET.RegisterServices;
    using Senparc.Weixin;
    using Senparc.Weixin.MP;
    using Senparc.Weixin.TenPay;
    using Senparc.Weixin.WxOpen;
    using FFF.Test.Application.Framework;
    using FFF.Test.DataAccess.Framework;
    using FFF.Test.DataAccess.Framework.Configuration;
    using FFF.Test.Util;
    using FFF.Test.Util.Configuration;
    using FFF.Test.Util.Consts;
    using FFF.Test.Util.Denpends;
    using FFF.Test.Util.Web;
    using AccessTokenContainer = Senparc.Weixin.MP.Containers.AccessTokenContainer;
    using Config = Senparc.Weixin.Config;

    [DependsOn(
           typeof(YudmAbpApplicationModule))]
    public class YdmCodeArtCoreNoMemeryTestModule : AbpModule
    {
        public override void PreInitialize()
        {
            Server.ContentRootPath = Path.GetFullPath(@"..\..\..\..\..\") + @"src\FFF.Test";

            // 初始化config的配置为了读取appjson文件
            IConfigurationRoot configuration = AppConfigurationHelper.Get(Server.ContentRootPath);
            AppConfigConsts.InitConfigBySettingJson(configuration);

            base.IocManager.IocContainer.Register(Component.For<IConfiguration>().UsingFactoryMethod(x => configuration));

            base.IocManager.IocContainer.Register();
            base.IocManager.Register<IHttpContextAccessor, HttpContextForDenpend>();
            this.Configuration.Modules.AbpEfCore().AddDbContext<MyDbContext>(options =>
            {
                if (options.ExistingConnection != null)
                {
                    DbContextOptionsConfigurer.Configure(options.DbContextOptions, options.ExistingConnection);
                }
                else
                {
                    DbContextOptionsConfigurer.Configure(options.DbContextOptions, options.ConnectionString);
                }
            });

            // this.InitWx();
        }

        public override void Initialize()
        {
            base.IocManager.RegisterAssemblyByConvention(typeof(YdmCodeArtCoreApplicationTestModule).GetAssembly());
            HibernatingRhinos.Profiler.Appender.EntityFramework.EntityFrameworkProfiler.Initialize();
        }

        private void InitWx()
        {
            // 初始化微信
            Config.SenparcWeixinSetting = AppConfigConsts.SenparcWeixinSetting;
            AppConfigConsts.SenparcSetting.IsDebug = true;

            //注册开始
            RegisterService register;

            var mockEnv = new Mock<IHostingEnvironment>();
            register = RegisterService.Start(mockEnv.Object, AppConfigConsts.SenparcSetting);

            var serviceCollection = new ServiceCollection();
            var configBuilder = new ConfigurationBuilder();
            var config = configBuilder.Build();
            serviceCollection.AddSenparcGlobalServices(config);
            serviceCollection.AddMemoryCache();
            register.UseSenparcGlobal(false);

            register.UseSenparcWeixin(AppConfigConsts.SenparcWeixinSetting, AppConfigConsts.SenparcSetting)
                   .RegisterMpAccount(AppConfigConsts.SenparcWeixinSetting, "【于大明】公众号")
                   .RegisterWxOpenAccount(AppConfigConsts.SenparcWeixinSetting, "【于大明】小程序")
                   .RegisterTenpayV3(AppConfigConsts.SenparcWeixinSetting, "【于大明】支付")
                   .RegisterMpAccount("微信公众号APPId", "微信公众号密钥", "微信公众号");

            // 测试是否注册成功
            // var tokenOpen = AccessTokenContainer.GetAccessToken(Config.SenparcWeixinSetting.WxOpenAppId);
            // var tokenWx = AccessTokenContainer.GetAccessToken(Config.SenparcWeixinSetting.WeixinAppId);
        }
    }
}